#include "workspace.h"
#include "mainwindow.h"

#include <newplotsetdialog.h>
#include <QMenu>

WorkSpace::WorkSpace(QWidget *parent) :
    QWidget(parent)
{
    layoutSetPara = VBOX; //Default Layout

    hboxLayout = new QHBoxLayout(); //Default Layout
    vboxLayout = new QVBoxLayout();
    setLayout(vboxLayout);
}


WorkSpace::~WorkSpace()
{
    delete vboxLayout;
    delete hboxLayout;
    for (int i = 0; i < plot.count(); ++i)
        delete plot.at(i);
    for (int i = 0; i < plotControl.count(); ++i)
        delete plotControl.at(i);
}

//--------------Begin Plot Managemant---------------------
void WorkSpace::newPlot(QList<GSData*>* dataSet)
{
    NewPlotSetDialog dialog(dataSet, this);
    int ok = dialog.exec();
    QString plotName = dialog.getPlotName();
    if(ok)
    {
        GSPlot::PlotType type;
        GSData dataX, dataY, dataZ;
        GSPlot* gsp;
        type = dialog.getPlotType();
        int plotHeight = dialog.getHeight();
        int plotWidth = dialog.getWidth();
        int resolution = dialog.getResolution();
        switch (type)
        {
            case GSPlot::V1D:
                dataX = *(dataSet->at(dialog.getXId()));
                gsp = new GSPlot(plotName, type, dataX,
                                 plotWidth, plotHeight,
                                 resolution);
                break;
            case GSPlot::V2D:
            case GSPlot::VPOLAR:
                dataX = *(dataSet->at(dialog.getXId()));
                dataY = *(dataSet->at(dialog.getYId()));
                gsp = new GSPlot(plotName, type, dataX, dataY,
                                 plotWidth, plotHeight,
                                 resolution);
                break;
            case GSPlot::V3D:
                dataX = *(dataSet->at(dialog.getXId()));
                dataY = *(dataSet->at(dialog.getYId()));
                dataZ = *(dataSet->at(dialog.getZId()));
                gsp = new GSPlot(plotName, type, dataX, dataY, dataZ,
                                 plotWidth, plotHeight,
                                 resolution);
                break;
        }
        plot.append(gsp);

        GSPlotControl* gsplotctl = new GSPlotControl(gsp);
        plotControl.append(gsplotctl);

        connect(gsplotctl, SIGNAL(plotCtlMenu(GSPlot*, GSPlotControl::CtlType)),
                this, SLOT(editPlot(GSPlot*,GSPlotControl::CtlType)) );
        updatePlotLayout();
    }
}

void WorkSpace::editPlot(GSPlot* gsp, GSPlotControl::CtlType type)
{
    int index = 0;
    for (int i = 0; i < plot.count(); i++)
        if(gsp == plot.at(i)) index = i;

    switch (type) {
    case GSPlotControl::DEL:
        removePlot(index);
        break;
    case GSPlotControl::BACK:
        if( index != plot.count() -1 ) //not last one
            movePlotBackward(index);
        break;
    case GSPlotControl::FORWARD:
        if(index != 0)  //not first one
            movePlotForward(index);
        break;
    default:
        break;
    }
}

void WorkSpace::removePlot(const int index)
{
    if( (index<0) || (index>=plot.count())) return;

    GSPlot* dplot = plot.at(index);
    plot.removeAt(index);
    delete dplot;

    GSPlotControl* gsplotctl = plotControl.at(index);
    plotControl.removeAt(index);
    delete gsplotctl;
    updatePlotLayout();
}

void WorkSpace::movePlotForward(const int index)
{
    plot.swap(index-1, index);
    plotControl.swap(index-1, index);
    updatePlotLayout();
}
void WorkSpace::movePlotBackward(const int index)
{
    plot.swap(index, index+1);
    plotControl.swap(index, index+1);
    updatePlotLayout();
}

//----------------End Plot Managemant---------------------


//--------------Begin Layout Managemant---------------------
void WorkSpace::setLayoutMode(LayoutSet layout)
{
    layoutSetPara = layout; //Default Layout
    updatePlotLayout();
}

void WorkSpace::layoutChange()
{
    setLayoutMode((layoutSetPara==VBOX) ? HBOX:VBOX );
}

WorkSpace::LayoutSet WorkSpace::getLayoutSet()
{
    return layoutSetPara;
}

void WorkSpace::updatePlotLayout()
{
    delete hboxLayout;
    delete vboxLayout;

    hboxLayout = new QHBoxLayout(); //Default Layout
    vboxLayout = new QVBoxLayout();
    for (int i = 0; i < plot.count(); i++)
    {
        GSPlot* gsp = plot.at(i);
        switch(gsp->plotType)
        {
        case GSPlot::V1D:
        case GSPlot::V2D:
            hboxLayout->addWidget(gsp->plot2d);
            vboxLayout->addWidget(gsp->plot2d);
            break;
        case GSPlot::V3D:
            hboxLayout->addWidget(gsp->plot3d);
            vboxLayout->addWidget(gsp->plot3d);
            break;
        case GSPlot::VPOLAR:
            hboxLayout->addWidget(gsp->plotPolar);
            vboxLayout->addWidget(gsp->plotPolar);
            break;
        default:
            break;
        }
    }

    switch (layoutSetPara)
    {
        case HBOX:
            setLayout(hboxLayout);
            break;
        case VBOX:
            setLayout(vboxLayout);
    }
    this->hide();
    this->show();    
}
//--------------End Layout Managemant---------------------

